package com.zscat.mallplus.util;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 * https://github.com/shenzhuan/mallplus on 2019/1/29.
 */
public class DateUtil {

    public static Calendar getRequiredDate(Date date, int count) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        // 时间加对应的天数
        cl.add(Calendar.DAY_OF_YEAR, count);
        return cl;
    }

    public static List<String> getNextSevenTime(Date date){
        List<String> dates = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        for (int i = 0;i<7;i++){
            Calendar time = getRequiredDate(date,i);
            String sdate = sdf.format(time.getTime());
            dates.add(sdate);
        }
        return dates;
    }

    public static String getWeek(String sdate){
        Date date = DateUtil.strToDate(sdate,"yyyyMMdd");
        String[] weeks = {"周日","周一","周二","周三","周四","周五","周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if(week_index<0){
            week_index = 0;
        }
        return weeks[week_index];
    }

    /**
     * 从Date类型的时间中提取日期部分
     */
    public static Date getDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    public static String getValueByType(String date, String type) {
        if ("year".equalsIgnoreCase(type)) {
            return StringUtils.substring(date, 0, 4);
        } else if ("month".equalsIgnoreCase(type)) {
            return StringUtils.substring(date, 4, 6);
        } else {
            return StringUtils.substring(date, 6, 8);
        }
    }

    /**
     * 从Date类型的时间中提取时间部分
     */
    public static Date getTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, 1970);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static String dateToStr(Date date, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        String s = "";

        try {
            s = format.format(date);
        } catch (Exception var5) {
            var5.printStackTrace();
        }

        return s;
    }

    public static Date strToDate(String str, String formatStr) {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Date date = null;

        try {
            date = format.parse(str);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return date;
    }

    /**
     * 获取本周日期
     *
     * @return
     */
    public static List<String> getCurrentTime(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = new SimpleDateFormat("yyyyMMdd").format(cal
                .getTime());
        List<String> times = new ArrayList<String>();
        times.add(imptimeBegin);
        String imptimeEnd = "";
        for (int i = 1; i < 7; i++) {
            cal.add(Calendar.DATE, 1);
            imptimeEnd = new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
            times.add(imptimeEnd);
        }
        return times;
    }

    /**
     * 获取上周日期
     * @return
     */
    public static List<String> getLastTimeInterval(Date date) {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar2.setTime(date);
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK)-1;
        if(dayOfWeek <= 0){
            dayOfWeek = 7;
        }
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        String lastBeginDate = new SimpleDateFormat("yyyyMMdd")
                .format(calendar1.getTime());
        String lastEndDate = new SimpleDateFormat("yyyyMMdd").format(calendar2
                .getTime());
        try {
            List<Date> dates = findDates(new SimpleDateFormat("yyyyMMdd").parse(lastBeginDate), new SimpleDateFormat("yyyyMMdd").parse(lastEndDate));
            List<String> sdates = new ArrayList<String>();
            for (Date date1 : dates) {
                String sdate = new SimpleDateFormat("yyyyMMdd").format(date1);
                sdates.add(sdate);
            }
            return sdates;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取下周日期
     * @return
     */
    public static List<String> getNextTimeInterval(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calendar1.setTime(date);
        calendar2.setTime(date);
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK)-1;
        if(dayOfWeek <= 0){
            dayOfWeek = 7;
        }
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 + 7);
        calendar2.add(Calendar.DATE, offset2 + 7);
        String lastBeginDate = sdf.format(calendar1.getTime());
        String lastEndDate = sdf.format(calendar2.getTime());
        try {
            List<Date> dates = findDates(new SimpleDateFormat("yyyyMMdd").parse(lastBeginDate), new SimpleDateFormat("yyyyMMdd").parse(lastEndDate));
            List<String> sdates = new ArrayList<String>();
            for (Date date1 : dates) {
                String sdate = new SimpleDateFormat("yyyyMMdd").format(date1);
                sdates.add(sdate);
            }
            return sdates;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static List<Date> findDates(Date dBegin, Date dEnd) {
        List<Date> lDate = new ArrayList();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (dEnd.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    public static String dateToString(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
}
